<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
//购买会员
if($_RQ['op']=='main'){
	set_html('title','购买点数');
	$basic=$this->plugin_setting('basic');
	$basic['dianum_rate']=$basic['dianum_rate']?:1;
	$post=array('fee'=>10,'checked'=>true,'num'=>10*$basic['dianum_rate']);
	$return['systemxieyi']=$this->plugin_setting('systemxieyi');
	$return['post']=$post;
	$return['pluginset']=$basic;
	$return['buybg']=$basic['buybg'];
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
		//创建一个会员订单
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'merchid'=>$merchid,
			'num'=>$postdata['num'],
			'ordersn'=>get_ordersn('jy_douyintui_dian_order','ordersn'),
			'fee'=>$postdata['fee'],
			'pay_id'=>0,
			'ispay'=>0,
			'status'=>0,
			'createtime'=>TIMESTAMP,
		);
		pdo_insert('jy_douyintui_dian_order',$insert);
		$id=pdo_insertid();
		$params=array(
			'id'=>$id,
			'title'=>'充值点数',
			'fee'=>$postdata['fee'],
			'plugin'=>'jy_douyintui',
			//'pay_type'=>'wechat',//可选，此项作用为指定支付方式
			'no_type'=>array('hdfk'),//可选，此项作用为指定不允许使用哪种支付方式
			'back_url'=>'jy_douyintui/merch.buydiannum/payresult/',//必填，指定异步支付结果通知地址，采用系统标准URL书写格式
			'success_url'=>$success_url?:'jy_douyintui/merch.dian_order/',
			'error_url'=>$error_url?:'jy_douyintui/merch.dian_order/',
		);
		$payid=cfc('pay')->create_pay_order($params);
		pdo_update('jy_douyintui_dian_order',array('pay_id'=>$payid),array('id'=>$id));
		$return=array('pay_id'=>$payid,'order_id'=>$id);
		exi($return);
}
if($_RQ['op']=='payresult'){
	$payresult=$_RQ['pay_result'];
	if(!$payresult){
		exi('支付失败，未收到支付结果！','error');
	}
	if($payresult['status']!=1){
		exi('支付失败！','error');
	}
	$order=pdo_get('jy_douyintui_dian_order',array('id'=>$payresult['id'],'uniacid'=>$_SESSION['uniacid']));
	if(!$order){
		exi('支付失败！购买会员订单不存在！','error');
	}
	if($order['fee']!=$payresult['fee']){
		exi('支付失败！购买会员订单金额不匹配！','error');
	}
	pdo_update('jy_douyintui_dian_order',array('status'=>1,'ispay'=>1),array('id'=>$order['id']));
	$item=array(
		'game_id'=>0,
		'merchid'=>$order['merchid']
	);
	$this->model('model')->add_dian_log($item,1,$order['num']);
	exi('支付成功','success','jy_douyintui/merch.index/');
}
